** Rachel Wellhausen (www.rwellhausen.com)
** .do file to reproduce quantitative results in text and appendix (last updated 22 Feb 2025)
** using Stata 18
** Necessary packages: 
*ssc install did_imputation

clear all
set more off
cd "XX" // appropriate command line
 
********************************************************************************
 ****** RESULTS *******
********************************************************************************
use "Data/Waste_HSmaster_Chinashock.dta", clear // Notes: Constitutent files to create this master dataset and documentation of merge process available on request.


****************************************
/* ****** COMMAND AND DATA PREP ******* */
****************************************
** China ban products
replace chinaban2017=0 if hs_code==262020 | hs_code==262050 | hs_code==262090 // out of use in 2017+, so not applicable
levelsof hs_code if chinaban2017==1, local(bancodes) // 

** Geographic treatment - SE Asia
gen asia_treat=0
bysort wbcode: replace asia_treat=1 if seasia==1 & chinaban2017==1 & highinc==0 & year>=2017
replace asia_treat=. if wbcode=="CHN"
label variable asia_treat "Treated: Bans + SE Asia"
codebook wbcode if asia_treat==1 // 41 states
codebook wbcode if asia_treat==0 // 227 states


** Geographic treatment hypothetical all bans? - SE Asia
gen asia_treat_all=0
label variable asia_treat_all "Treated: Real/intended bans + SE Asia"
bysort wbcode: replace asia_treat_all=1 if seasia==1 & chinaban_all==1 & highinc==0 & year>=2017 

** Sample: highinc = 0
** Cluster: on hs_id
** FE
local fe i.waste_catNUM i.wbcodeNUM i.year // 
** Locals for covariates
gen tempimp = impusd_hs * 1000000 // unscaling original variables (in millions) to avoid errors
gen tempexp = expusd_hs * 1000000 // unscaling original variables (in millions) to avoid errors
bysort hs_code year: gen hsval_usd = tempimp + tempexp
label var hsval_usd "Product market size (unscaled USD)"
gen hsval_usdln = ln(hsval_usd + ((hsval_usd^2 +1)^0.5))
label var hsval_usdln "Product market size (USD, ln)"

local demand  l.hsval_usdln  // nonhighinc_imp toxic = collinear, not needed 
local control l.gdppc l.gdppc2 l.industry l.natres l.tradeln l.wdi_unemploy l.dem l.wto  // 



********************************************************************************
/* ****** FIGURE: PARALLEL TRENDS PLOTS *******
********************************************************************************

/* ** Pure trends (179 products) */
** backfill product-state units that get treated in 2017
gen backfill=0
replace backfill=1 if hs_code==261900 & seasia==1 & highinc==0
replace backfill=1 if hs_code==262011 & seasia==1 & highinc==0
replace backfill=1 if hs_code==262019 & seasia==1 & highinc==0
replace backfill=1 if hs_code==262021 & seasia==1 & highinc==0
replace backfill=1 if hs_code==262029 & seasia==1 & highinc==0
replace backfill=1 if hs_code==262030 & seasia==1 & highinc==0
replace backfill=1 if hs_code==262040 & seasia==1 & highinc==0
replace backfill=1 if hs_code==262060 & seasia==1 & highinc==0
replace backfill=1 if hs_code==262091 & seasia==1 & highinc==0
replace backfill=1 if hs_code==262099 & seasia==1 & highinc==0
replace backfill=1 if hs_code==391510 & seasia==1 & highinc==0
replace backfill=1 if hs_code==391520 & seasia==1 & highinc==0
replace backfill=1 if hs_code==391530 & seasia==1 & highinc==0
replace backfill=1 if hs_code==391590 & seasia==1 & highinc==0
replace backfill=1 if hs_code==470790 & seasia==1 & highinc==0
replace backfill=1 if hs_code==510310 & seasia==1 & highinc==0
replace backfill=1 if hs_code==510320 & seasia==1 & highinc==0
replace backfill=1 if hs_code==510330 & seasia==1 & highinc==0
replace backfill=1 if hs_code==510400 & seasia==1 & highinc==0
replace backfill=1 if hs_code==520210 & seasia==1 & highinc==0
replace backfill=1 if hs_code==520291 & seasia==1 & highinc==0
replace backfill=1 if hs_code==520299 & seasia==1 & highinc==0
replace backfill=1 if hs_code==550510 & seasia==1 & highinc==0
replace backfill=1 if hs_code==550520 & seasia==1 & highinc==0
replace backfill=1 if hs_code==631010 & seasia==1 & highinc==0
replace backfill=1 if hs_code==631090 & seasia==1 & highinc==0

collapse (mean) tariffln (firstnm) countryname if highinc==0 & wbcode!="CHN", by(backfill year)
set scheme white_tableau
twoway (connected tariffln year if backfill==1) (connected tariffln year if backfill==0), ///
    xline(2017) ///
    legend(label(1 "Treated") label(2 "Untreated") position(6) col(2)) ///
    title("Sample: 179 waste products", size(small)) ///
		    graphregion(margin(2 2 2 2)) ///
    plotregion(margin(2 2 2 2)) ///
    xsize(1.2) ysize(2)
graph play "Figures/paralleltrends.grec"
graph export "Figures/paralleltrends_pure.png", as(png) replace 


/* ** Subsample threatened products (44 products) */
gen backfill=0
replace backfill=1 if hs_code==261900 & seasia==1 & highinc==0
replace backfill=1 if hs_code==262011 & seasia==1 & highinc==0
replace backfill=1 if hs_code==262019 & seasia==1 & highinc==0
replace backfill=1 if hs_code==262021 & seasia==1 & highinc==0
replace backfill=1 if hs_code==262029 & seasia==1 & highinc==0
replace backfill=1 if hs_code==262030 & seasia==1 & highinc==0
replace backfill=1 if hs_code==262040 & seasia==1 & highinc==0
replace backfill=1 if hs_code==262060 & seasia==1 & highinc==0
replace backfill=1 if hs_code==262091 & seasia==1 & highinc==0
replace backfill=1 if hs_code==262099 & seasia==1 & highinc==0
replace backfill=1 if hs_code==391510 & seasia==1 & highinc==0
replace backfill=1 if hs_code==391520 & seasia==1 & highinc==0
replace backfill=1 if hs_code==391530 & seasia==1 & highinc==0
replace backfill=1 if hs_code==391590 & seasia==1 & highinc==0
replace backfill=1 if hs_code==470790 & seasia==1 & highinc==0
replace backfill=1 if hs_code==510310 & seasia==1 & highinc==0
replace backfill=1 if hs_code==510320 & seasia==1 & highinc==0
replace backfill=1 if hs_code==510330 & seasia==1 & highinc==0
replace backfill=1 if hs_code==510400 & seasia==1 & highinc==0
replace backfill=1 if hs_code==520210 & seasia==1 & highinc==0
replace backfill=1 if hs_code==520291 & seasia==1 & highinc==0
replace backfill=1 if hs_code==520299 & seasia==1 & highinc==0
replace backfill=1 if hs_code==550510 & seasia==1 & highinc==0
replace backfill=1 if hs_code==550520 & seasia==1 & highinc==0
replace backfill=1 if hs_code==631010 & seasia==1 & highinc==0
replace backfill=1 if hs_code==631090 & seasia==1 & highinc==0

collapse (mean) tariffln (firstnm) countryname if highinc==0 & wbcode!="CHN" & chinaban_all==1, by(backfill year)
set scheme white_tableau
twoway (connected tariffln year if backfill==1) (connected tariffln year if backfill==0), ///
    xline(2017) ///
    legend(label(1 "Treated") label(2 "Untreated") position(6) col(2)) ///
    title("Sample: 44 waste products (banned + threatened)", size(small)) ///
	    graphregion(margin(2 2 2 2)) ///
    plotregion(margin(2 2 2 2)) ///
    xsize(1.2) ysize(2)
graph play "Figures/paralleltrends.grec"
graph export "Figures/paralleltrends_banthreat.png", as(png) replace 

*/


********************************************************************************
/* ****** ANALYSIS: CHINA BAN DID ******* 
********************************************************************************

xtset 
eststo clear
** Treatment: SE Asia + banned, sample: All products
_eststo, title(""): xtdidregress (tariffln) (asia_treat) if highinc==0 & wbcode!="CHN", group(hs_id) time(year) vce(cluster hs_id) // 
codebook hs_code if e(sample) // 
codebook wbcode if e(sample) // 

** Treatment: SE Asia + banned, comparison group: only China intent-to-treat waste prodcuts
_eststo, title(""): xtdidregress (tariffln) (asia_treat) if highinc==0 & wbcode!="CHN" & chinaban_all==1, group(hs_id) time(year) vce(cluster hs_id) // 
codebook hs_code if e(sample) // 
codebook wbcode if e(sample) // 

** Treatment: SE Asia + banned, sample: All products
_eststo, title(""): xtdidregress (tariffln `demand' `control' `fe') (asia_treat) if highinc==0 & wbcode!="CHN", group(hs_id) time(year) vce(cluster hs_id) // 
codebook hs_code if e(sample) // 
codebook wbcode if e(sample) // 

** Treatment: SE Asia + banned, comparison group: only China intent-to-treat waste prodcuts
_eststo, title(""): xtdidregress (tariffln `demand' `control' `fe') (asia_treat) if highinc==0 & wbcode!="CHN" & chinaban_all==1, group(hs_id) time(year) vce(cluster hs_id) // 
codebook hs_code if e(sample) // 
codebook wbcode if e(sample) //

esttab using "Output/did_main.tex", replace style(tex) nomtitles page float label compress nobaselevels noomitted nonotes se ar2  ///
star(* 0.1 ** 0.05 *** 0.01) ///
keep( *.asia_treat) ///
title("Higher tariffs on imports treated by China garbage shock") ///
stats(N N_clust, fmt(%9.0gc %9.0gc %9.3f) labels("Observations" "Clusters")) ///
addnotes("SE clustered by state-product. * p $<$ 0.1, ** p $<$ 0.05, *** p $<$ 0.01.")

*/


********************************************************************************
/* ****** ROBUSTNESS: CHINA BAN DID INCLUDING POTENTIAL POST-TREATMENT CONTROLS ******* 
********************************************************************************

xtset 
eststo clear
** Treatment: SE Asia + banned, sample: All products
_eststo, title(""): xtdidregress (tariffln `demand' `control' l.basel l.wtr_01 `fe') (asia_treat) if highinc==0 & wbcode!="CHN", group(hs_id) time(year) vce(cluster hs_id) // 
codebook hs_code if e(sample) // 
codebook wbcode if e(sample) // 

** Treatment: SE Asia + banned, comparison group: only China intent-to-treat waste prodcuts
_eststo, title(""): xtdidregress (tariffln `demand' `control' l.basel l.wtr_01 `fe') (asia_treat) if highinc==0 & wbcode!="CHN" & chinaban_all==1, group(hs_id) time(year) vce(cluster hs_id) // 
codebook hs_code if e(sample) // 
codebook wbcode if e(sample) //

esttab using "Output/did_ntm.tex", replace style(tex) nomtitles page float label compress nobaselevels noomitted nonotes se ar2  ///
star(* 0.1 ** 0.05 *** 0.01) ///
keep( *.asia_treat) ///
title("Robustness: Controlling for non-tariff measures") ///
stats(N N_clust, fmt(%9.0gc %9.0gc %9.3f) labels("Observations" "Clusters")) ///
addnotes("SE clustered by state-product. * p $<$ 0.1, ** p $<$ 0.05, *** p $<$ 0.01.")

*/


********************************************************************************
/* ****** ROBUSTNESS: TARIFF WATER + CHINA BAN DID ******* 
********************************************************************************

xtset 
eststo clear

** generate bound tariff variables
replace bound_max=. if wto==0 // eliminating errors (none).
gen bound_maxln = ln(bound_max + ((bound_max^2 +1)^0.5))
label var bound_maxln "Bound maximum tariff (ln)"
gen dv_bound_error = bound_max - tariff
label variable dv_bound_error "Distance from bound tariff, w violations" // smaller distance = higher tariff
gen dv_bound=dv_bound_error
*sum dv_bound_error if dv_bound_error<0 & highinc==0, d  // 1545 errors, or di 1545/101139 = 1.5% of observations of Global South bound tariffs
replace dv_bound=0 if dv_bound_error<0 & dv_bound_error!=. // replacing errors (tariff reported higher than binding) with 0 (evidence that tariff binding is met/tariff water is minimized)
gen dv_boundln = ln(dv_bound + ((dv_bound^2 +1)^0.5))
label var dv_boundln "Tariff water (ln)" // distaance from bound level. Smaller value = closer to bound tariff.

** Treatment: SE Asia + banned, sample: All products, DV tariff water
_eststo, title(""): xtdidregress (dv_boundln l.bound_maxln `fe' `demand' `control') (asia_treat) if highinc==0 & wbcode!="CHN", group(hs_id) time(year) vce(cluster hs_id) // 
codebook hs_code if e(sample) // 
codebook wbcode if e(sample) // 


** Treatment: SE Asia + banned, comparison group: only China intent-to-treat waste prodcuts
_eststo, title(""): xtdidregress (dv_boundln l.bound_maxln `fe' `demand' `control') (asia_treat) if highinc==0 & wbcode!="CHN" & chinaban_all==1, group(hs_id) time(year) vce(cluster hs_id) // 
codebook hs_code if e(sample) // 
codebook wbcode if e(sample) //

esttab using "Output/did_boundtariffrobust.tex", replace style(tex) nomtitles page float label compress nobaselevels noomitted nonotes se ar2  ///
star(* 0.1 ** 0.05 *** 0.01) ///
keep( *.asia_treat) ///
title("Less tariff water on imports treated by China garbage shock, given WTO tariff binding") ///
stats(N N_clust, fmt(%9.0gc %9.0gc %9.3f) labels("Observations" "Clusters")) ///
addnotes("SE clustered by state-product. * p $<$ 0.1, ** p $<$ 0.05, *** p $<$ 0.01.")

*/


********************************************************************************
/* ******* DESCRIPTIVE INFORMATION AND TARIFF HEAT MAPS *******
***************

/* Descriptive statistics - trade in 26 waste products pre-ban 
use "Data/Waste_HSmaster_Chinashock.dta", clear 
collapse (sum) impkg_hs impusd_hs if highinc==0 & year<2017 & year>2013 & chinaban2017==1, by(countryname)
*/


** Generating tariff values
use "Data/Waste_HSmaster_Chinashock.dta", clear 
gen tariff_adj=tariff
replace tariff_adj=100 if tariff>=100 & tariff!=. // 402 tariffs capped off at 100
gen tariff_adjln = ln(tariff_adj + ((tariff_adj^2 +1)^0.5)) // inverse hyperbolic sine; https://marcfbellemare.com/wordpress/12856
label variable tariff_adjln "Tariff^ (ln)"
sum tariff_adj, d

** Drop wbcodes without any tariffs reported
drop if wbcode=="ASM" 
drop if wbcode=="AND" 
drop if wbcode=="BES" 
drop if wbcode=="IOT" 
drop if wbcode=="VGB" 
drop if wbcode=="CXR" 
drop if wbcode=="CCK" 
drop if wbcode=="CUW" 
drop if wbcode=="FLK" 
drop if wbcode=="ATF" 
drop if wbcode=="GIB" 
drop if wbcode=="GRL" 
drop if wbcode=="GUM" 
drop if wbcode=="IRQ" 
drop if wbcode=="KIR" 
drop if wbcode=="MHL" 
drop if wbcode=="MYT" 
drop if wbcode=="FSM" 
drop if wbcode=="ANT" 
drop if wbcode=="NCL" 
drop if wbcode=="NIU" 
drop if wbcode=="NFK" 
drop if wbcode=="PRK" 
drop if wbcode=="MNP" 
drop if wbcode=="PSE" 
drop if wbcode=="PCN" 
drop if wbcode=="BLM" 
drop if wbcode=="SHN" 
drop if wbcode=="SPM" 
drop if wbcode=="SMR" 
drop if wbcode=="SXM" 
drop if wbcode=="SOM" 
drop if wbcode=="SSD" 
drop if wbcode=="TKL" 
drop if wbcode=="TCA" 

** Only Global South
drop if highinc==1
sort wbcode

** generate categories to order y axis by region
gen regcon=region + countryname if wbcode!=""
sort regcon


** For presentation - categories/HS codes
tostring hs_code, gen(hs_codeSTR)
replace hs_codeSTR= "0" + "50100" if hs_codeSTR=="50100" 
replace hs_codeSTR= "0" + "50210" if hs_codeSTR=="50210" 
replace hs_codeSTR= "0" + "50290" if hs_codeSTR=="50290" 
replace hs_codeSTR= "0" + "50300" if hs_codeSTR=="50300" 
replace hs_codeSTR= "0" + "50510" if hs_codeSTR=="50510" 
replace hs_codeSTR= "0" + "50590" if hs_codeSTR=="50590" 
replace hs_codeSTR= "0" + "50690" if hs_codeSTR=="50690" 
replace hs_codeSTR= "0" + "50710" if hs_codeSTR=="50710" 
replace hs_codeSTR= "0" + "50790" if hs_codeSTR=="50790" 
replace hs_codeSTR= "0" + "50800" if hs_codeSTR=="50800"
sort hs_codeSTR
gen catcombo = waste_cat + "_" + hs_codeSTR

** Make figures
** MIN
sort wbcode catcombo year
heatplot tariff_adjln regcon catcombo, ysc(reverse) xtitle("Waste products, ordered by type", size(vsmall)) ytitle("Country, alpha by region", size(vsmall)) colors(scico lajolla) levels(20) statistic(min) recenter  keylabels(minmax) title("Minimum tariff, by country-waste product (1995-2020)", span size(vsmall)) ysize(5) xsize(11)
graph play "Figures/heatmap_tariff.grec"
graph export "Figures/heatmap_min.png", as(png) replace

** MAX
heatplot tariff_adjln regcon catcombo, ysc(reverse) xtitle("Waste products, ordered by type", size(vsmall)) ytitle("Country, alpha by region", size(vsmall)) colors(scico lajolla) levels(20) statistic(max) recenter keylabels(minmax) title("Maximum tariff, by country-waste product (1995-2020)", span size(vsmall))
graph play "Figures/heatmap_tariff.grec"
graph export "Figures/heatmap_max.png", as(png) replace




*/


********************************************************************************
/* ****** FIGURE: TARIFF OVER TIME TREND ******* 
********************************************************************************

set scheme white_tableau
use "Data/Waste_HSmaster_Chinashock.dta", clear
keep if highinc==0 // Global South only
keep if year>=1996 // Drop 1995 - missing data interferes with clarity in visualization
bysort wbcode: ipolate wdi_weightedmeantariff_all year, gen(tariff_zavg)
label variable tariff_zavg "Weighted mean applied tariff (interpolated)"
gen tariff_chinaban=tariff if chinaban2017==1
label variable tariff_chinaban "China ban tariff"

gen tariff_plastic=tariff if cat_plastic==1
gen tariff_animal=tariff if cat_animal==1
gen tariff_metal=tariff if cat_metal==1
gen tariff_mineral=tariff if cat_mineral==1
gen tariff_paper=tariff if cat_paper==1
gen tariff_textile=tariff if cat_textile==1
gen tariff_vegetable=tariff if cat_vegetable==1
gen tariff_chemical=tariff if cat_chemical==1

collapse (mean) tariff_*, by(year)
aorder

label variable tariff_plastic "Plastic waste (7 products)"
label variable tariff_animal "Animal waste (29 products)"
label variable tariff_metal "Metal waste (65 products)"
label variable tariff_mineral "Mineral waste (3 products)"
label variable tariff_paper "Paper waste (4 products)"
label variable tariff_textile "Textile waste (22 products)"
label variable tariff_vegetable "Vegetable waste (29 products)"
label variable tariff_chemical "Chemical waste (20 products)"
label variable tariff_zavg "Avg. tariff level, all products"

** All waste products, by category
set scheme white_tableau
graph twoway (line tariff_animal year, lwidth(medium) lpattern(dash) ) ///
    (line tariff_chemical year, lwidth(medium) lpattern(shortdash) ) ///
    (line tariff_metal year, lwidth(medium) lpattern(solid) ) ///
    (line tariff_mineral year, lwidth(thin) lpattern(solid) ) ///
    (line tariff_paper year, lwidth(medium) lpattern(longdash) ) ///
    (line tariff_plastic year, lwidth(thick) lpattern(shortdash) ) ///
    (line tariff_textile year, lwidth(medium) lpattern(longdash) ) ///
    (line tariff_vegetable year, lwidth(thin) lpattern(solid) ) ///
    (lowess tariff_zavg year, lwidth(thick) lcolor(gs9) lpattern(dot)) ///
    , ylabel(, grid labsize(vsmall) angle(0)) ///
    xlabel(1996 2000 2005 2010 2015 2017 2020, grid labsize(vsmall) angle(0) labcolor(black)) ///
    legend(label(1 "`: variable label tariff_animal'") ///
           label(2 "`: variable label tariff_chemical'") ///
           label(3 "`: variable label tariff_metal'") ///
           label(4 "`: variable label tariff_mineral'") ///
           label(5 "`: variable label tariff_paper'") ///
           label(6 "`: variable label tariff_plastic'") ///
           label(7 "`: variable label tariff_textile'") ///
           label(8 "`: variable label tariff_vegetable'") ///
           label(9 "Avg. tariff level") ///	   
           position(1) ring(0) cols(1) size(vsmall)) ///
    graphregion(color(white)) bgcolor(white) ///
    note("Notes: Observations report the average across Global South states. Smoothed line is weighted mean applied tariff for all traded products.", size(tiny) span) ///
    ytitle("% Tariff", size(vsmall)) ///
    xtitle("", size(small)) ///
    xline(2017, lwidth(thin) lcolor(gs7) lpattern(dot)) /// Add vertical line at x = 2017
    ysize(7) xsize(5)
graph export "Figures/tariff_wastecat.png", as(png) replace

 */


